<?php
namespace app\server\controller;
use app\server\controller\Event;
use think\Controller;
use Workerman\Worker;

class Workman extends Controller
{

     public  function  web_socket_server(){
         $ws_worker = new Worker("websocket://0.0.0.0:9501");
// 启动4个进程对外提供服务
         $ws_worker->count = 4;
// 当收到客户端发来的数据后返回hello $data给客户端
         $ws_worker->onMessage = function($connection, $data)
         {
             // 向客户端发送hello $data
           //   $connection->send('hello ' . $data);
             $data = json_decode($data,true);
             switch ($data['type']){
                 case 'send_all_message':
                     echo "全服发送邮件\n";
                     //调用邮件发送方法
                     $event = new Event();
                     $result =   $event->email_send_all($data);
                     echo json_encode($result)."\n";
                     break;
                 default:
                     break;
             }
         };
         $ws_worker->onConnect = function ($connection){

         };

         $ws_worker->onClose = function ($connection){
             echo "关闭连接\n";
         };
// 运行worker
         Worker::runAll();
     }


}
